package com.wlts.wzy.LMGoodsModule.updateUtil;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.linkedmall.model.v20180116.QueryAddressRequest;
import com.aliyuncs.linkedmall.model.v20180116.QueryAddressResponse;
import com.wlts.wzy.Base.BaseEntity;
import com.wlts.wzy.LMGoodsModule.entity.LmAddress;
import com.wlts.wzy.LMGoodsModule.service.ILmAddressService;
import com.wlts.wzy.configBean.LmConfig.LmConfig;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import com.wlts.wzy.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.stream.Collectors;

import static com.wlts.wzy.configBean.LmConfig.LmConfig.LZ_BIZ_ID;

@Component
public class LmAddressUpdate {
    @Autowired
    private ILmAddressService addressService;
    private static final IAcsClient client = LmConfig.client;

    //    @Scheduled(fixedDelay = 120 * 1000)
    //刷新地址库
  //  @PostConstruct
    public void updateLmAddress(){
        getAddress("1");
        updateLmAddressByLevel(2);
        updateLmAddressByLevel(3);
        updateLmAddressByLevel(4);
        updateLmAddressByLevel(5);
    }


    private void updateLmAddressByLevel(int level){
        List<LmAddress> lmAddresses = addressService.list(new MyLambdaQueryWrapper<LmAddress>().eq(LmAddress::getDivisionLevel, level).eq(BaseEntity::getDelFlag, "0"));

        lmAddresses.forEach(lmAddress -> {
            getAddress(lmAddress.getDivisionCode() + "");
        });
    }


    private void getAddress(String DivisionCode) {

        //request.setItemId(itemId);
        //  request.setPageSize(10);
        QueryAddressRequest request = new QueryAddressRequest();
        request.setBizId(LZ_BIZ_ID);
        request.setDivisionCode(DivisionCode);

        try {
            QueryAddressResponse resp = client.getAcsResponse(request);
            System.out.println(JSON.toJSONString(resp));

            List<QueryAddressResponse.DivisionAddressItem> divisionAddress = resp.getDivisionAddress();

            List<LmAddress> addressList = divisionAddress.stream().map(item -> {

                MyLambdaQueryWrapper<LmAddress> queryWrapper = new MyLambdaQueryWrapper<>();

                queryWrapper.eq(LmAddress::getDivisionCode, item.getDivisionCode());
                queryWrapper.eq(LmAddress::getDivisionLevel, item.getDivisionLevel());
                queryWrapper.eq(LmAddress::getDelFlag, "0");

                LmAddress address = addressService.getOne(queryWrapper);
                if (address == null) {
                    address = new LmAddress();
                }
                CopyUtils.copyProperties(item, address);
                return address;
            }).filter(lmAddress -> lmAddress.getId() == null).collect(Collectors.toList());

            addressService.saveBatch(addressList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
